﻿var commonPage = {
    _table_enableHilight: true,
    _table_container_selector: ".common-table-container",
    _table_selector: ".common-table-container" + " .table",
    _table_header_selector: ".common-table-container" + " .table" + " .common-table-header",
    _table_data_order_name_selector: ".common-table-container" + " .table" + " .common-table-header" + " input[data-order-name='']",
    _table_data_order_direction_selector: ".common-table-container" + " .table" + " .common-table-header" + " input[data-order-direction='']",
    _table_data_order_th_selector: ".common-table-container" + " .table" + " .common-table-header" + " th[data-order-field]",
    _table_checkbox_all_selector: ".common-table-container" + " .table" + " .common-table-header" + " .common-table-checkbox-all",
    _table_checkbox_radio_selector: ".common-table-container" + " .table" + " tbody tr input[type='checkbox'], tbody tr input[type='radio']",
    _table_checkbox_selector: ".common-table-container" + " .table" + " tbody tr input[type='checkbox']",
    _table_radio_selector: ".common-table-container" + " .table" + " tbody tr input[type='radio']",
    _page_layout_part: [".panel-Title", ".titlePanel", ".search-container",".gm-pagination",],

    initialize: function (vueInstance) {
        this.initializeTable(vueInstance);
    },

    /* 初始化Table
    ----------------------------------------------------------*/
    initializeTable: function (vueInstance) {
        if (this.getJTable().length == 0)
            return;

        this.bindVue(vueInstance);

        this.resizeTable();

    },
    /* 动态添加布局
    ----------------------------------------------------------*/
    addPart: function (partSelector) {
        this._page_layout_part[this._page_layout_part.length] = partSelector;
        this.resizeTable();
    },

    bindVue: function (vueInstance) {
        //$("th[data-order-field]").off('click').on('click',
        //    function () {
        //        commonPage.initializeFieldOrder($(this).attr("data-order-field"), vueInstance);
        //    });

        ////回车搜索
        //$(document).keypress(function (e) {
        //    if (e.which === 13 && vueInstance.getList != undefined) {
        //        vueInstance.getList();
        //    }
        //});

        ////重置按钮事件
        //$(".btn-clear").bind("click", function () {
        //    commonPage.initializeClear(vueInstance);
        //});

        ////搜索按钮事件
        //$(".btn-search").bind("click", function () {
        //    vueInstance.searchEntity.PageIndex = 0;
        //    vueInstance.getList();
        //});

    },

    getJTable: function () {
        return $(this._table_selector);
    },

    initializeInputDisplay: function () {
        $(".decimal-input").bind("paste", function () { return false; });
        $(".decimal-input").forceDecimal();
        $(".decimal-input").bind("blur", function () {
            if ($.trim($(this).val()) == "") {
                return;
            }
            $(this).val($(this).val().replace(/,/gi, ''));

            var thisValue = $.trim($(this).val());
            if (thisValue.substring(thisValue.length - 1) == ".") {
                thisValue = ",";
            }
            if (isNaN(thisValue)) {
                $(this).val("");
                var docusDomID = document.activeElement.id;
                $(this).focus();
                $("#" + docusDomID).focus();
                $(this).blur();
            }

            $(this).val(parseFloat($(this).val()));
        });

        /**
         * 设置只能输入整数
         */
        $(".numeric-input").bind("paste", function () { return false; });
        $(".numeric-input").forceNumeric();
        $(".numeric-input").bind("blur", function () {

            if ($(this).val() == "") {
                return;
            }
            else {
                //去除千分位
                $(this).val($(this).val().replace(/,/gi, ''));

                var val = parseInt($(this).val());
                if (val > parseInt($(this).attr("maxValue")) || val < parseInt($(this).attr("minValue"))) {
                    $(this).val("");
                    var docusDomID = document.activeElement.id;
                    $(this).focus();
                    $("#" + docusDomID).focus();
                    $(this).blur();
                }
            }
            if (isNaN($(this).val())) {
                $(this).val("");
                var docusDomID = document.activeElement.id;
                $(this).focus();
                $("#" + docusDomID).focus();
                $(this).blur();
            }
            $(this).val(parseInt($(this).val()));
        });

        /**
         * 数字格式化
         */
        $('.decimal').each(
            function () {
                $(this).val(window.formatNumber($(this).val(), 2));
            }
        );
        $('.numeric').each(
            function () {
                $(this).val(window.formatNumber($(this).val(), 0));
            }
        );

        /**
         * 日期选择，强制readonly
         */
        $(".date-picker").datepicker({})
            .bind("paste", function () { return false; })
            .bind("mouseup", function () { $(this).focus(); $(this).select(); })
            .forceReadOnly();

        $('.datetime-picker,.input-datetime').datetimepicker({
            step: 30,
            format: "Y-m-d H:i:s"
        });

        /**
         * 日期格式化
         */
        $(".shortdate").each(function (idx, elem) {
            if ($(elem).is(":input")) {
                if ("0001-01-01" != $.dateFormat.date($(elem).val(), "yyyy-MM-dd")) {
                    $(elem).val($.dateFormat.date($(elem).val(), "yyyy-MM-dd"));
                } else {
                    $(elem).val('');
                }

            }
            else {
                $(elem).text($.dateFormat.date($(elem).text(), "yyyy-MM-dd"));
            }
        });
        $(".longdate").each(function (idx, elem) {
            if ($(elem).is(":input")) {
                if ("0001-01-01 12:00:00" != $.dateFormat.date($(elem).val(), "yyyy-MM-dd hh:mm:ss")) {
                    $(elem).val($.dateFormat.date($(elem).val(), "yyyy-MM-dd HH:mm:ss"));
                } else {
                    $(elem).val('');
                }
            } else {
                $(elem).text($.dateFormat.date($(elem).text(), "yyyy-MM-dd HH:mm:ss"));
            }
        });

    },


    initializeSearchCondition: function (vue) {
        var searchEntityWrapper = {};
        delete vue.searchEntity.EnumOrderDirection;

        jQuery.each(vue.searchEntity, function (i, val) {
            searchEntityWrapper["SearchEntity." + i] = val;
        });
        return searchEntityWrapper;
    },

    initializeClear: function (vue) {
        vue.searchEntity = {};
    },

    exportToExcel: function (type, vue) {
        vue.exportType = type;
        vue.getList();
    },
    
    initializeExport: function (searchEntityWrapper, vue) {
        searchEntityWrapper.ExportType = vue.exportType;
        $("#aDownload").remove();
        $("<a></a>", {
            'class': "a-exportEXL",
            'id': 'aDownload',
            'style': 'display:none',
            'href': vue.apiUrl + "/ExportToExcel?" + $.param(searchEntityWrapper)
        }).appendTo("body");

        document.getElementById("aDownload").click();
        vue.exportType = null;
    },

    /**
     * 重新计算外侧frame高度
     * @returns {} 
     */
    resizeTable: function () {
        this.setTableHeight();
    },

    /* 动态设置table的高度*/
    setTableHeight: function () {
        var size = {};
        size.y = $(window).height();
        var m1= $(".gm-pagination").css("margin-top").split('px')[0];
        var m2= $('body').css("margin-top").split('px')[0];

        size.y = size.y - m1 * 2 - m2 - 2;
        for (var i = 0; i < this._page_layout_part.length; i++) {
            if ($(this._page_layout_part[i]).is(":visible")) {
                size.y = size.y - $(this._page_layout_part[i]).outerHeight();
            }
        }
        $(this._table_container_selector).css("height", (size.y) + "px");
    },    
};



